
package edu.chl.vrangogym.beans.ctrl;

import edu.chl.vrangogym.beans.backing.BookingBackingBean;
import edu.chl.vrangogym.persistence.entities.Booking;
import edu.chl.vrangogym.persistence.entities.User;
import edu.chl.vrangogym.persistence.exception.NonexistentEntityException;
import edu.chl.vrangogym.persistence.impl.UserFacade;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author habbbe
 */
@ManagedBean(name = "BookingCtrl")
@ViewScoped
public class BookingCtrlBean implements Serializable {
    
    @ManagedProperty(value="#{BookingBackingBean}")
    BookingBackingBean bookingBackingBean;
    
    public void setBookingBackingBean(BookingBackingBean bookingBackingBean){
        this.bookingBackingBean = bookingBackingBean;
    }
    
    
    @EJB
    private UserFacade userFacade;


    /** Creates a new instance of LoginCtrlBean */
    public BookingCtrlBean() {
    }
    
    @PostConstruct
    public void postConstruct() {
    }

 
    /**
     * Add a selected booking for the current user
     */
    public void addBooking() {
        
        User user = (User)FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().get("Auth.user");
        
        User managedUser = userFacade.find(user.getUserName());
        
        Booking b = new Booking(user, bookingBackingBean.getSelectedWorkoutSession(), bookingBackingBean.getSelectedNumberOfParticipant());
        
        managedUser.addBooking(b);
        
        try {
            userFacade.edit(managedUser);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(BookingBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
 
}
